// 初始化音频上下文
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
// 创建音频元素
const audioElement = new Audio();
// 设置音频源
const source = audioContext.createMediaStreamDestination();

// 创建MediaSource
const mediaSource = new MediaSource();
audioElement.src = URL.createObjectURL(mediaSource);
mediaSource.addEventListener('sourceopen', onSourceOpen);

let sourceBuffer;

function onSourceOpen() {
  // 创建SourceBuffer并指定编码类型
  // sourceBuffer = mediaSource.addSourceBuffer('audio/wav; codecs="1"');
  sourceBuffer = mediaSource.addSourceBuffer('audio/wav; codecs="opus"');
  sourceBuffer.addEventListener('updateend', () => {
    if (!sourceBuffer.updating && mediaSource.readyState === 'open') {
      // 如果所有数据都已经添加并且MediaSource仍然处于打开状态，则结束流
      mediaSource.endOfStream();
    }
  });

  console.log("sourceBuffer")
  console.log(sourceBuffer)
  // 将音频上下文的输出连接到音频元素
  source.connect(audioContext.destination);
}


function appendBuffer()
{
  // 添加音频数据到SourceBuffer
  sourceBuffer.appendBuffer(data);

}


export { appendBuffer }